import {
  LockOutlined,
  UserOutlined,

} from '@ant-design/icons';
import {message, Tabs} from 'antd';
import React, {useState} from 'react';
import {ProFormText, LoginForm} from '@ant-design/pro-form';
import {history} from 'umi';
import Footer from '@/components/Footer';
import {register} from '@/services/ant-design-pro/api';

import styles from './index.less';
import {SYSTEM_LOGO, USER_CENTER_FONT_GITEE_SRC} from "@/constants";
// import {Simulate} from "react-dom/test-utils";
// import error = Simulate.error;


const Register: React.FC = () => {
  const [type, setType] = useState<string>('account');


  /**
   * ------------------------------------------------------------
   * 表单提交
   * @param values 注册参数
   */
  const handleSubmit = async (values: API.RegisterParams) => {
    try {
      //将values中的值取出来
      const {userPassword, checkPassword} = values;

      //校验两次输入的密码是否相同
      if (userPassword !== checkPassword) {
        message.error('两次输入密码不相同');
        return;
      }

      //注册后返回用户信息
      const id = await register({...values, type});

      //如果user这个对象存不为空
      // @ts-ignore
      if (id) {

        const defaultLoginSuccessMessage = '注册成功！';
        message.success(defaultLoginSuccessMessage);
        /** 此方法会跳转到 redirect 参数所在的位置 */

        if (!history) return;
        const {query} = history.location;

        //下面这一段的逻辑是：如果被拦截了就重定向到query所在的页面
        history.push({
          pathname:'/user/login',
          query
        });
        return;
      }

      // setUserLoginState(id);
      // eslint-disable-next-line @typescript-eslint/no-shadow
    } catch (error:any) {
      const defaultLoginFailureMessage = '注册失败，请重试！';
      message.error( defaultLoginFailureMessage );
    }
  };

  //------------------------------------------------------------

  return (
    <div className={styles.container}>
      <div className={styles.content}>
        <LoginForm
          logo={<img alt="logo" src={SYSTEM_LOGO}/>}
          title="User-Center"
          //修改提交按钮
          submitter={{
            searchConfig: {
              submitText: '注册'
            }
          }}
          subTitle={<a href={USER_CENTER_FONT_GITEE_SRC} target={"_blank"} rel="noreferrer">好用的用户中心</a>}
          initialValues={{
            autoLogin: true,
          }}
          onFinish={async (values) => {
            await handleSubmit(values as API.LoginParams);
          }}
        >
          <Tabs activeKey={type} onChange={setType}>
            <Tabs.TabPane key="account" tab={'账号密码注册'}/>
          </Tabs>

          {type === 'account' && (
            <>
              <ProFormText
                name="userAccount"
                fieldProps={{
                  size: 'large',
                  prefix: <UserOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'请输入账号'}
                rules={[
                  {
                    required: true,
                    message: '账号是必填项！',
                  },
                ]}
              />



              <ProFormText.Password
                name="userPassword"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'请输入密码'}
                rules={[
                  {
                    required: true,
                    message: '密码是必填项！',
                  },
                  {
                    min: 8,
                    message: '密码不少于8位 ！',
                  },
                ]}
              />
              <ProFormText.Password
                name="checkPassword"
                fieldProps={{
                  size: 'large',
                  prefix: <LockOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'请输入确认密码'}
                rules={[
                  {
                    required: true,
                    message: '确认密码是必填项！',
                  },
                  {
                    min: 8,
                    message: '确认密码不少于8位 ！',
                  },
                ]}
              />
              <ProFormText
                name="planetCode"
                fieldProps={{
                  size: 'large',
                  prefix: <UserOutlined className={styles.prefixIcon}/>,
                }}
                placeholder={'请输入星球编号'}
                rules={[
                  {
                    required: true,
                    message: '星球编号是必填项！',
                  },
                ]}
              />
            </>
          )}

          <div
            style={{
              marginBottom: 24,
            }}
           />
        </LoginForm>
      </div>
      <Footer/>
    </div>
  );
};

export default Register;
